package com.ruoyi.li.work.service.impl;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.li.work.domain.PlatformUUserDept;
import com.ruoyi.li.work.mapper.PlatformUUserDeptMapper;
import com.ruoyi.li.work.mapper.SysDeptMapper;

import com.ruoyi.li.work.mapper.SysUserMapper;
import com.ruoyi.li.work.service.PlatformUUserDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 王旭
 * @since 2024-06-13
 */
@Service
public class PlatformUUserDeptServiceImpl extends ServiceImpl<PlatformUUserDeptMapper, PlatformUUserDept> implements PlatformUUserDeptService {

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public AjaxResult selectPlatformUUserDeptList(PlatformUUserDept platformUUserDept) {
        return null;
    }

    @Override
    public AjaxResult selectPlatformUUserDeptOwn() {
        // 获取当前用户ID
        Long userId = SecurityUtils.getUserId();
        if (userId == null) {
            return new AjaxResult(HttpStatus.UNAUTHORIZED, "用户未登录", null);
        }

        // 获取老师所关联的班级列表
        String deptIds = sysUserMapper.selectUserById(userId).getClassId();
        List<String> stringClassIds = StringUtils.str2List(deptIds, ",", true, true);
        if (CollectionUtils.isEmpty(stringClassIds)) {
            return new AjaxResult(HttpStatus.SUCCESS, "您当前暂未分配班级", new ArrayList<>());
        }

        // 获取班级信息
        List<SysDept> sysDepts = new ArrayList<>();
        for (String stringClassId : stringClassIds) {
            long l = Long.parseLong(stringClassId);
            SysDept sysDept = sysDeptMapper.selectDeptById(l);
            if (sysDept != null) {
                sysDepts.add(sysDept);
            }
        }

        return new AjaxResult(HttpStatus.SUCCESS, "查询成功", sysDepts);
    }


    @Override
    public AjaxResult insertPlatformUUserDept(PlatformUUserDept platformUUserDept) {
        return null;
    }

    @Override
    public AjaxResult updatePlatformUUserDept(PlatformUUserDept platformUUserDept) {
        return null;
    }

    @Override
//    @Transactional(rollbackFor = RuntimeException.class)
    public AjaxResult deletePlatformUUserDeptByIds(Integer[] ids) {
        return null;
    }
}
